package com.peng.leetcode.other;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * IntervalMerge
 *
 * @author: lupeng6
 * @create: 2020/11/28 22:21
 */
public class IntervalMerge {

    public static void main(String[] args) {
//        [[1,3],[2,6],[8,10],[15,18]]
//        System.out.println(new Solution().merge());
//        Deque<Integer> dq = new LinkedList();
//        dq.push(1);
//        dq.push(2);
//        dq.push(3);
//        while (!dq.isEmpty()) {
//            System.out.println(dq.pop());
//        }

        Deque<Integer> dq = new LinkedList();
        dq.add(1);
        dq.add(2);
        dq.add(3);
        while (!dq.isEmpty()) {
            System.out.println(dq.pop());
        }
    }

    class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null) {
                return res;
            }
            Deque<TreeNode> dq = new LinkedList();
//            dq.offer()
            dq.add(root);
            while (!dq.isEmpty()) {
                List<Integer> level = new ArrayList<>();
                for (int i = 0; i < dq.size(); i++) {
                    TreeNode node = dq.pop();
                    level.add(node.val);
                    if (node.left != null) {
                        dq.push(node.left);
                    }
                    if (node.right != null) {
                        dq.push(node.right);
                    }
                }
                res.add(level);
            }
            return res;
        }
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
